'\" t
.TH "SD_EVENT_ADD_MEMORY_PRESSURE" "3" "" "systemd 255" "sd_event_add_memory_pressure"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.ad l
.\" -----------------------------------------------------------------
.\" * MAIN CONTENT STARTS HERE *
.\" -----------------------------------------------------------------
.SH "NAME"
sd_event_add_memory_pressure, sd_event_source_set_memory_pressure_type, sd_event_source_set_memory_pressure_period, sd_event_trim_memory \- Add and configure an event source run as result of memory pressure
.SH "SYNOPSIS"
.sp
.ft B
.nf
#include <systemd/sd\-event\&.h>
.fi
.ft
.sp
.ft B
.nf
typedef struct sd_event_source sd_event_source;
.fi
.ft
.HP \w'int\ sd_event_add_memory_pressure('u
.BI "int sd_event_add_memory_pressure(sd_event\ *" "event" ", sd_event_source\ **" "ret_source" ", sd_event_handler_t\ " "handler" ", void\ *" "userdata" ");"
.HP \w'int\ sd_event_source_set_memory_pressure_type('u
.BI "int sd_event_source_set_memory_pressure_type(sd_event_source\ *" "source" ", const\ char\ *" "type" ");"
.HP \w'int\ sd_event_source_set_memory_pressure_period('u
.BI "int sd_event_source_set_memory_pressure_period(sd_event_source\ *" "source" ", uint64_t\ " "threshold_usec" ", uint64_t\ " "window_usec" ");"
.HP \w'int\ sd_event_trim_memory('u
.BI "int sd_event_trim_memory(void);"
.SH "DESCRIPTION"
.PP
\fBsd_event_add_memory_pressure()\fR
adds a new event source that is triggered whenever memory pressure is seen\&. This functionality is built around the Linux kernel\*(Aqs
\m[blue]\fBPressure Stall Information (PSI)\fR\m[]\&\s-2\u[1]\d\s+2
logic\&.
.PP
Expects an event loop object as first parameter, and returns the allocated event source object in the second parameter, on success\&. The
\fIhandler\fR
parameter is a function to call when memory pressure is seen, or
\fBNULL\fR\&. The handler function will be passed the
\fIuserdata\fR
pointer, which may be chosen freely by the caller\&. The handler may return negative to signal an error (see below), other return values are ignored\&. If
\fIhandler\fR
is
\fBNULL\fR, a default handler that compacts allocation caches maintained by
libsystemd
as well as glibc (via
\fBmalloc_trim\fR(3)) will be used\&.
.PP
To destroy an event source object use
\fBsd_event_source_unref\fR(3), but note that the event source is only removed from the event loop when all references to the event source are dropped\&. To make sure an event source does not fire anymore, even if it is still referenced, disable the event source using
\fBsd_event_source_set_enabled\fR(3)
with
\fBSD_EVENT_OFF\fR\&.
.PP
If the second parameter of
\fBsd_event_add_memory_pressure()\fR
is
\fBNULL\fR
no reference to the event source object is returned\&. In this case the event source is considered "floating", and will be destroyed implicitly when the event loop itself is destroyed\&.
.PP
The event source will fire according to the following logic:
.sp
.RS 4
.ie n \{\
\h'-04' 1.\h'+01'\c
.\}
.el \{\
.sp -1
.IP "  1." 4.2
.\}
If the
\fI$MEMORY_PRESSURE_WATCH\fR/\fI$MEMORY_PRESSURE_WRITE\fR
environment variables are set at the time the event source is established, it will watch the file, FIFO or AF_UNIX socket specified via
\fI$MEMORY_PRESSURE_WATCH\fR
(which must contain an absolute path name) for
\fBPOLLPRI\fR
(in case it is a regular file) or
\fBPOLLIN\fR
events (otherwise)\&. After opening the inode, it will write the (decoded) Base64 data provided via
\fI$MEMORY_PRESSURE_WRITE\fR
into it before it starts polling on it (the variable may be unset in which case this is skipped)\&. Typically, if used,
\fI$MEMORY_PRESSURE_WATCH\fR
will contain a path such as
/proc/pressure/memory
or a path to a specific
memory\&.pressure
file in the control group file system (cgroupfs)\&.
.RE
.sp
.RS 4
.ie n \{\
\h'-04' 2.\h'+01'\c
.\}
.el \{\
.sp -1
.IP "  2." 4.2
.\}
If these environment variables are not set, the local PSI interface file
memory\&.pressure
of the control group the invoking process is running in is used\&.
.RE
.sp
.RS 4
.ie n \{\
\h'-04' 3.\h'+01'\c
.\}
.el \{\
.sp -1
.IP "  3." 4.2
.\}
If that file does not exist, the system\-wide PSI interface file
/proc/pressure/memory
is watched instead\&.
.RE
.PP
Or in other words: preferably any explicit configuration passed in by an invoking service manager (or similar) is used as notification source, before falling back to local notifications of the service, and finally to global notifications of the system\&.
.PP
Well\-behaving services and applications are recommended to react to memory pressure events by executing one or more of the following operations, in order to ensure optimal behaviour even on loaded and resource\-constrained systems:
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
Release allocation caches such as
\fBmalloc_trim()\fR
or similar, both implemented in the libraries consumed by the program and in private allocation caches of the program itself\&.
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
Release any other form of in\-memory caches that can easily be recovered if needed (e\&.g\&. browser caches)\&.
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
Terminate idle worker threads or processes, or similar\&.
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
Even exit entirely from the program if it is idle and can be automatically started when needed (for example via socket or bus activation)\&.
.RE
.PP
Any of the suggested operations should help easing memory pressure situations and allowing the system to make progress by reclaiming the memory for other purposes\&.
.PP
This event source typically fires on memory pressure stalls, i\&.e\&. when operational latency above a configured threshold already has been seen\&. This should be taken into consideration when discussing whether later latency to re\-aquire any released resources is acceptable: it\*(Aqs usually more important to think of the latencies that already happened than those coming up in future\&.
.PP
The
\fBsd_event_source_set_memory_pressure_type()\fR
and
\fBsd_event_source_set_memory_pressure_period()\fR
functions can be used to fine\-tune the PSI parameters for pressure notifications\&. The former takes either
"some",
"full"
as second parameter, the latter takes threshold and period times in microseconds as parameters\&. For details about these three parameters see the PSI documentation\&. Note that these two calls must be invoked immediately after allocating the event source, as they must be configured before polling begins\&. Also note that these calls will fail if memory pressure parameterization has been passed in via the
\fI$MEMORY_PRESSURE_WATCH\fR/\fI$MEMORY_PRESSURE_WRITE\fR
environment variables (or in other words: configuration supplied by a service manager wins over internal settings)\&.
.PP
The
\fBsd_event_trim_memory()\fR
function releases various internal allocation caches maintained by
libsystemd
and then invokes glibc\*(Aqs
\fBmalloc_trim\fR(3)\&. This makes the operation executed when the handler function parameter of
\fBsd_event_add_memory_pressure\fR
is passed as
\fBNULL\fR
directly accessible for invocation at any time (see above)\&. This function will log a structured log message at
\fBLOG_DEBUG\fR
level (with message ID f9b0be465ad540d0850ad32172d57c21) about the memory pressure operation\&.
.PP
For further details see
\m[blue]\fBMemory Pressure Handling in systemd\fR\m[]\&\s-2\u[2]\d\s+2\&.
.SH "RETURN VALUE"
.PP
On success, these functions return 0 or a positive integer\&. On failure, they return a negative errno\-style error code\&.
.SS "Errors"
.PP
Returned errors may indicate the following problems:
.PP
\fB\-ENOMEM\fR
.RS 4
Not enough memory to allocate an object\&.
.sp
Added in version 254\&.
.RE
.PP
\fB\-EINVAL\fR
.RS 4
An invalid argument has been passed\&.
.sp
Added in version 254\&.
.RE
.PP
\fB\-EHOSTDOWN\fR
.RS 4
The
\fI$MEMORY_PRESSURE_WATCH\fR
variable has been set to the literal string
/dev/null, in order to explicitly disable memory pressure handling\&.
.sp
Added in version 254\&.
.RE
.PP
\fB\-EBADMSG\fR
.RS 4
The
\fI$MEMORY_PRESSURE_WATCH\fR
variable has been set to an invalid string, for example a relative rather than an absolute path\&.
.sp
Added in version 254\&.
.RE
.PP
\fB\-ENOTTY\fR
.RS 4
The
\fI$MEMORY_PRESSURE_WATCH\fR
variable points to a regular file outside of the procfs or cgroupfs file systems\&.
.sp
Added in version 254\&.
.RE
.PP
\fB\-EOPNOTSUPP\fR
.RS 4
No configuration via
\fI$MEMORY_PRESSURE_WATCH\fR
has been specified and the local kernel does not support the PSI interface\&.
.sp
Added in version 254\&.
.RE
.PP
\fB\-EBUSY\fR
.RS 4
This is returned by
\fBsd_event_source_set_memory_pressure_type()\fR
and
\fBsd_event_source_set_memory_pressure_period()\fR
if invoked on event sources at a time later than immediately after allocating them\&.
.sp
Added in version 254\&.
.RE
.PP
\fB\-ESTALE\fR
.RS 4
The event loop is already terminated\&.
.sp
Added in version 254\&.
.RE
.PP
\fB\-ECHILD\fR
.RS 4
The event loop has been created in a different process, library or module instance\&.
.sp
Added in version 254\&.
.RE
.PP
\fB\-EDOM\fR
.RS 4
The passed event source is not a signal event source\&.
.sp
Added in version 254\&.
.RE
.SH "NOTES"
.PP
Functions described here are available as a shared library, which can be compiled against and linked to with the
\fBlibsystemd\fR\ \&\fBpkg-config\fR(1)
file\&.
.PP
The code described here uses
\fBgetenv\fR(3), which is declared to be not multi\-thread\-safe\&. This means that the code calling the functions described here must not call
\fBsetenv\fR(3)
from a parallel thread\&. It is recommended to only do calls to
\fBsetenv()\fR
from an early phase of the program when no other threads have been started\&.
.SH "HISTORY"
.PP
\fBsd_event_add_memory_pressure()\fR,
\fBsd_event_source_set_memory_pressure_type()\fR,
\fBsd_event_source_set_memory_pressure_period()\fR, and
\fBsd_event_trim_memory()\fR
were added in version 254\&.
.SH "SEE ALSO"
.PP
\fBsystemd\fR(1),
\fBsd-event\fR(3),
\fBsd_event_new\fR(3),
\fBsd_event_add_io\fR(3),
\fBsd_event_add_time\fR(3),
\fBsd_event_add_child\fR(3),
\fBsd_event_add_inotify\fR(3),
\fBsd_event_add_defer\fR(3),
\fBsd_event_source_set_enabled\fR(3),
\fBsd_event_source_set_description\fR(3),
\fBsd_event_source_set_userdata\fR(3),
\fBsd_event_source_set_floating\fR(3)
.SH "NOTES"
.IP " 1." 4
Pressure Stall Information (PSI)
.RS 4
\%https://docs.kernel.org/accounting/psi.html
.RE
.IP " 2." 4
Memory Pressure Handling in systemd
.RS 4
\%https://systemd.io/MEMORY_PRESSURE
.RE
